package yf.platform.biz;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.yfsoft.comm.util.StringUtil;

import yf.platform.bo.BizResult;
import yf.platform.ctl.GlobalKey;

public class AjaxService extends TplService {
	
	/**
	 * 通过模型ID获取模型的数据
	 * 
	 */
	@SuppressWarnings("unchecked")
	public BizResult rows(Map<String,String> input) 
			throws Throwable{
		Map<String,Object> user = rst.getSissAttribute(GlobalKey.USER);
		String m_id = input.get("m_id");
		Map<String,Object> m = null;
		if("admin".equals(user.get("ui_username"))){
			m = this.daoSupport.findOne("sys_model",null,"m_id = "+m_id);
		}else{
			m = this.daoSupport.findOne("sys_model m,mdl_security ms,sys_r_sg_sr gr","m.*,gr.*",StringUtil.format(" m.m_id = ms.ts_model and ms.ts_sg = gr.r_g and gr.r_r = (select o_role from usr_obs where o_id = {0}) and m.m_id = {1}", user.get("ui_dept"),m_id));
		}
		rst.setReqAttribute("model", m);
		List<Map<String,Object>> controls = this.daoSupport.find("sys_model_control mc,sys_control c", 
				"mc.*,c.*",
				"mc.mc_control_id = c.c_id and mc.mc_model_id = "+m_id,
				"mc_index");
		if(controls.size()<1){
			return rst.setCode(-10)
				.setMsg("未设置栏位信息");
		}
		rst.setReqAttribute("controls", controls);
		
		String where = null;
		if("1".equals(m.get("m_private").toString())){
			where = m.get("m_user").toString() + " = " +user.get("ui_id"); 
		}
		//获取到用户的配置信息
		Map<String,String> profile = (Map<String,String>)user.get("ui_profile");
		
		String view = m.get("m_view").toString();
		//根据用户的配置信息来决定视图的排序方式
		List<Map<String,Object>> entrys = this.daoSupport.find(view,input.get("pri")+" as DT_RowId"+input.get("cols")+((m.get("m_upload").toString().equals("1"))?",guid":""),where,profile.containsKey(view)?profile.get(view):"");
		for(Map<String,Object> c : controls){
			Object ds_id = c.get("mc_ds");
			if(null == ds_id) continue;
			if("0".equals(ds_id)) continue;
			for(int i = 0 ;i < entrys.size();i++){
				Map<String,Object> e = entrys.get(i);
				e.put(c.get("mc_attr_name").toString(),
						this.getDataValue(Integer.parseInt(ds_id.toString()),e.get(c.get("mc_attr_name")))
						);
				entrys.set(i, e);
			}
		}
		this.isajax = true;
		return rst.setCode(0)
			.setType(BizResult.ECHO)
			.setData(entrys);
	}
	
	
	/**
	 * 通过模型ID来获取模型设置时的表格头部信息
	 * 
	 * @param input
	 * @return
	 * @throws Throwable
	 */
	public BizResult list(Map<String,String> input) 
			throws Throwable{
		Map<String,Object> user = rst.getSissAttribute(GlobalKey.USER);
		String m_id = input.get("m_id");
		Map<String,Object> m = null;
		//加载权限信息
		if("admin".equals(user.get("ui_username"))){
			m = this.daoSupport.findOne("sys_model",null,"m_id = "+m_id);
		}else{
			m = this.daoSupport.findOne("sys_model m,mdl_security ms,sys_r_sg_sr gr","m.*,gr.*",StringUtil.format(" m.m_id = ms.ts_model and ms.ts_sg = gr.r_g and gr.r_r = (select o_role from usr_obs where o_id = {0}) and m.m_id = {1}", user.get("ui_dept"),m_id));
		}
		rst.setReqAttribute("model", m);
		
		//加载模型对应的表头信息
		List<Map<String,Object>> controls = this.daoSupport.find("sys_model_control mc,sys_control c", 
				"mc.*,c.*",
				"mc.mc_control_id = c.c_id and mc.mc_model_id = "+m_id,
				"mc_index");
		if(controls.size()<1){
			return rst.setCode(-10)
				.setMsg("未设置栏位信息");
		}
		
		JSONArray cols = new JSONArray();
		StringBuilder sb = new StringBuilder();
		for(Map<String,Object> c:controls){
			if("1".equals(c.get("mc_pri").toString().trim())) {
				rst.setReqAttribute("pri", c.get("mc_attr_name"));
				continue;
			}
			if("0".equals(c.get("mc_show").toString().trim())) continue;
			JSONObject o = new JSONObject();
			o.put("data", c.get("mc_attr_name"));
			sb.append(",").append(c.get("mc_attr_name"));
			Object ds = c.get("mc_ds");
			if(null != ds){
				JSONObject obj = new JSONObject();
				List<Map<String,Object>> datasource = this.getDataSource(c);
				if(null != datasource){
					for(Map<String,Object> ds_m:datasource){
						obj.put(ds_m.get("k"), ds_m.get("v"));
					}
				}
				c.put("mc_ds", obj.toString());
			}
			cols.add(o);
		}
		rst.setReqAttribute("controls", controls);
		rst.setReqAttribute("cols", sb.toString());
		rst.setReqAttribute("columns", cols);
		return rst.setCode(0)
				.setType(BizResult.REDIRECT)
				.setPage("WIDGETS/TPL/table.jsp");
	}

	/**
	 * 删除一条记录
	 */
	public BizResult delete(Map<String,String> input){
		int rows = this.daoSupport.delete(input.get("view"), input.get("pri")+"="+input.get("id"));
		this.isajax = true;
		return rst.setCode(0).setType(BizResult.ECHO);
	}
	
	public BizResult edit(Map<String,String> input)throws Throwable{
		rst = super.edit(input);
		
		return rst.setCode(0).setType(BizResult.ECHO);
	}
}

